package cn.ljs.android.util.ui.util;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Point;
import android.os.Build;
import android.view.Display;
import android.view.WindowManager;

/**
 * Created by Json on 6/1/2016.
 */
public class AppUtils {

    private static int screenWidth = 0;
    private static int screenHeight = 0;

    /**
     * 获取屏幕宽度
     * @param context context
     * @return 返回屏幕像素宽度
     */
    public static int getSreenWidth(Context context) {
        initScreenSize(context);
        return screenWidth;
    }

    /**
     * 获取屏幕高度
     * @param context context
     * @return 返回屏幕像素高度
     */
    public static int getScreenHeight(Context context) {
        initScreenSize(context);
        return screenHeight;
    }

    /**
     * dp转px
     * @param dp dp值
     * @return 返回px值
     */
    public static int dp2Px(int dp) {
        return (int) (dp * Resources.getSystem().getDisplayMetrics().density);
    }

    private static void initScreenSize(Context context) {
        if(0 == screenWidth || 0 == screenHeight) {
            WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            Display display = wm.getDefaultDisplay();
            Point size = new Point();
            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
                display.getSize(size);
                screenWidth = size.x;
                screenHeight = size.y;
            } else {
                screenWidth = display.getWidth();
                screenHeight = display.getHeight();
            }
        }
    }
}
